package com.yanqu.road.dao.impl.channelcenter.washe;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.washe.config.WasheSceneConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TempWasheSceneConfigDaoImpl extends TempNoPoolCacheDao {

    public TempWasheSceneConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    private static String getWasheSceneConfigSql = "select * from `t_n_washepvp_scene` where `activityId`=?";

    public Map<Integer, WasheSceneConfig> getWasheSceneConfigMap(int activityId) {

        SqlCacheParam sqlCacheParam = getCacheParam(getWasheSceneConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, WasheSceneConfig>) cacheResult;
        }

        Map<Integer, WasheSceneConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheSceneConfig washeSceneConfig = new WasheSceneConfig();
                washeSceneConfig.setActivityId(rs.getInt("activityId"));
                washeSceneConfig.setSceneId(rs.getInt("sceneId"));
                washeSceneConfig.setParam(rs.getString("param"));
                washeSceneConfig.setSceneName(rs.getString("sceneName"));
                washeSceneConfig.setSceneDesc(rs.getString("sceneDesc"));
                washeSceneConfig.setIsOpen(rs.getInt("isOpen"));

                // init
                washeSceneConfig.setParamList(StringUtils.stringToLongList(washeSceneConfig.getParam(), "\\|"));

                map.put(washeSceneConfig.getSceneId(), washeSceneConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, map);
        return map;
    }

}
